<%@tag description="put the tag description here" pageEncoding="UTF-8"%>
<%@ include file="/taglibs.jsp" %>

<%@attribute name="total" type="java.lang.Integer"%>
<%@attribute name="pagesize" type="java.lang.Integer"%>
<%@attribute name="page" type="java.lang.Integer"%>
<%@attribute name="requestURI"%>
<%@attribute name="includeEnds"%>
<%@attribute name="range" type="java.lang.Integer"%>


<%
    int pages = total / pagesize;
    if (total % pagesize != 0) {
        pages = pages + 1;
    }

    page = Math.max(1, Math.min(pages, page));

    int dp = range / 2;
    int startPage = Math.max(1, page - dp);
    int endPage = Math.min(startPage + range - 1, pages);
    if (endPage - startPage < range) {
        startPage = Math.max(1, endPage - (range - 1));
    }
    String baseURI = requestURI;
    if (!baseURI.contains("?")) {
        baseURI = baseURI + "?";
    } else {
        baseURI = baseURI + "&";
    }
    baseURI = baseURI + "page=";

    jspContext.setAttribute("startPage", startPage);
    jspContext.setAttribute("endPage", endPage);
    jspContext.setAttribute("page", page);
    jspContext.setAttribute("atStart", page == 1);
    jspContext.setAttribute("atEnd", page == pages);
    jspContext.setAttribute("baseURI", baseURI);

%>
<c:if test="${!atStart}">
    <s:url var="url" value="${requestURI}">
        <s:param name="page" value="${page - 1}"/>
    </s:url>
    <a class="prevPageIndicator" href="${url}">Prev</a>
</c:if>
<c:if test="${atStart}">
    <span class="prevPageIndicatorDisabled">Prev</span>
</c:if>
<c:forEach begin="${startPage}" end="${endPage}" var="p">
    <c:choose>
        <c:when test="${p == page}">
            <span class="curPageIndicator">${p}</span>
        </c:when>
        <c:otherwise>
        <s:url var="url" value="${requestURI}">
            <s:param name="page" value="${p}"/>
        </s:url>
        <a class="gotoPageIndicator" href="${url}">${p}</a>
        </c:otherwise>
    </c:choose>
</c:forEach>
<c:if test="${!atEnd}">
    <s:url var="url" value="${requestURI}">
        <s:param name="page" value="${page + 1}"/>
    </s:url>
    <a class="nextPageIndicator" href="${url}">Next</a>
</c:if>
<c:if test="${atEnd}">
    <span class="nextPageIndicatorDisabled">Next</span>
</c:if>
